package indi.zhifa.engine.core.common.web.utils;

import indi.zhifa.engine.core.common.utils.CommonUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class WebCommonUtil extends CommonUtil {

    public static boolean isDev(){
        return "dev".equals(SpringUtil.getProperty("spring.profiles.active")) || "local".equals(SpringUtil.getProperty("spring.profiles.active"));
    }

    public static boolean isTest(){
        return "test".equals(SpringUtil.getProperty("spring.profiles.active"));
    }

    public static boolean isProd(){
        return "prod".equals(SpringUtil.getProperty("spring.profiles.active"));
    }

    static public HttpServletRequest getServletRequest(){
        RequestAttributes requestAttributes = null;
        try{
            requestAttributes = RequestContextHolder.currentRequestAttributes();
        }catch (IllegalStateException ex){
            return null;
        }
        if(!(requestAttributes instanceof ServletRequestAttributes)){
            return null;
        }
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
        if(null == servletRequestAttributes){
            return null;
        }
        HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
        return httpServletRequest;
    }
}
